<script lang="tsx">
/**
 * 项目客户管理
 */
export default {
  name: "develop-project-customer"
}
</script>

<script lang="tsx" setup>
import {ref} from "vue";
import {definePageModel, PageModel, processRequest} from "@/framework";
import {ElMessage, ElMessageBox} from "element-plus";
import APIS from "@/apis";

/**
 * 页面模型
 */
const pageModelRef = ref()
const config = definePageModel<any>({
  url:APIS.DEVELOP_PROJECT_CUSTOMER_BASE,
  otherParams: {
    id: null
  },
  searchForm: {
    beforeSubmit(data) {
      if (data.endTime) {
        data.endTime += " 23:59:59"
      }
      return data
    },
    els: [
      {
        eType: "el-input",
        prop: "code",
        props: {
          placeholder: "客户编码",
          clearable: true
        }
      },
      {
        eType: "el-input",
        prop: "name",
        props: {
          placeholder: "客户姓名",
          clearable: true
        }
      },
      {
        eType: "el-input",
        prop: "phone",
        props: {
          placeholder: "客户联系方式",
          clearable: true
        }
      },
      {
        eType: "el-date-picker",
        prop: "startTime",
        props: {
          valueFormat: "YYYY-MM-DD",
          placeholder: "创建时间-起始",
          clearable: true
        }
      },
      {
        eType: "el-date-picker",
        prop: "endTime",
        props: {
          valueFormat: "YYYY-MM-DD",
          placeholder: "创建时间-结束",
          clearable: true
        }
      },
    ]
  },
  programForm: {
    els: [
      {
        label: "新增",
        eType: "el-button",
        props: {
          type: "primary"
        },
        event: "add"
      },
      {
        label: "删除",
        eType: "el-button",
        props: {
          type: "danger",
        },
        event: "delete-multi"
      },
    ]
  },
  table: {
    page: {
      enable: true,
      pageSize: 20
    },
    selection: true,
    operation: {
      width: 100,
      els: [
        {
          eType: "el-button",
          label: "编辑",
          event: "edit",
          props: {
            type: "primary",
            link: true
          }
        },
        {
          eType: "el-button",
          label: "删除",
          event: "delete",
          props: {
            type: "primary",
            link: true
          }
        },
      ]
    },
    props: {
      stripe: true,
      border: true
    },
    els: [
      {
        label: "客户编码",
        prop: "code",
        minWidth: 120
      },
      {
        label: "客户名称",
        prop: "name",
        minWidth: 120,
      },
      {
        label: "客户联系方式",
        prop: "phone",
        minWidth: 120,
      },
      {
        label: "客户备注",
        prop: "remark",
        minWidth: 160,
      },
      {
        label: "创建人姓名",
        prop: "createStaffName",
        minWidth: 120,
      },
      {
        label: "创建时间",
        prop: "createTime",
        minWidth: 140,
        renderFn(data) {
          return <span>{data.createTime?.toDate()}</span>
        },
      }
    ]
  },
  form: {
    title: "新增/编辑",
    width: 500,
    sheetForm: {
      labelWidth: "110px",
      required: ['code', 'name', 'phone'],
      els: [
        {
          label: "客户编码",
          prop: "code",
          eType: "el-input",
          props: {
            placeholder: "客户编码",
            clearable: true
          },
        },
        {
          label: "客户名称",
          prop: "name",
          eType: "el-input",
          props: {
            placeholder: "客户名称",
            clearable: true
          },
        },
        {
          label: "客户联系方式",
          prop: "phone",
          eType: "el-input",
          props: {
            placeholder: "客户联系方式",
            clearable: true
          },
        },
        {
          label: "备注",
          prop: "remark",
          eType: "el-input",
          props: {
            placeholder: "备注",
            clearable: true,
            type: "textarea",
            rows: 3
          }
        }
      ]
    }
  }
})

/**
 * 刷新
 */
const handleRefresh = (clear: boolean = false) => {
  pageModelRef.value.refreshTableData()
  clear && pageModelRef.value.clearSelection()
}

/**
 * 新增
 */
const handleAdd = () => {
  pageModelRef.value.handleAdd()
}

/**
 * 编辑
 */
const handleEdit = (row: any) => {
  pageModelRef.value.handleEdit(row)
}

/**
 * 删除
 */
const handleDelete = (row: any) => {
  pageModelRef.value.handleDelete(row)
}

/**
 * 删除 - 批量
 */
const handleDeleteMulti = async () => {
  const selectData = pageModelRef.value.getTableSelectData()
  if (!selectData) {
    ElMessage.warning("请先选择一条数据")
    return false
  }
  await ElMessageBox.confirm("是否确认删除?", "删除", {
    type: "error"
  })
  await processRequest.post('/api/DevelopProjectCustomer/DeleteMulti', {
    ids: selectData.map((item: any) => item.id)
  })

  ElMessage.success("删除成功")
  handleRefresh()
}
</script>

<template>
  <PageModel ref="pageModelRef" :PageModel="config" @add="handleAdd" @delete="handleDelete"
             @edit="handleEdit" @delete-multi="handleDeleteMulti"/>
</template>

<style lang="scss" scoped>
</style>